package com.only.common.util;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.asymmetric.KeyType;

import java.util.Objects;

/**
 * 密码处理器, 如果密码为空，返回 “”
 *
 * @author only
 * @date 2023/11/8
 * @description
 */
public class PasswordHandler {

    public static final String InvalidPassword = "******";

    public static final String PublicKey = "042274BCDD7C583372BCF12112A544882DE69B968FCB92DCA18E4362412EF3AC89535CC06D19BAB811C0598A0AA87B8B6CD9E413F6A56629A9317C4E2C0A72E517";
    public static final String PrivateKey = "009BE715BACA516A6C18395212866F1817CD409822A0379EDD87EED7828284288A";


    /**
     * 判断为无效的密码
     * @param password
     * @return
     */
    public static boolean isInvalidPassword(String password) {
        return Objects.equals(password, InvalidPassword);
    }

    public static String encrypt(String password) {
        if (StrUtil.isBlank(password)) return "";

        return SmUtil.sm2(null, PublicKey).encryptHex(password, KeyType.PublicKey);
    }

    public static String decrypt(String password) {
        if (StrUtil.isBlank(password)) return "";

        return SmUtil.sm2(PrivateKey, null).decryptStr(password, KeyType.PrivateKey);
    }
}
